package org.svgweb.core;


	import flash.xml.XML;
	import haxe.PosInfos;
	import haxe.Stack;
	import haxe.Timer;
	import me.cunity.debug.Out;
    import org.svgweb.nodes.SVGSVGNode;

    import flash.display.Sprite;
    import flash.events.ContextMenuEvent;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.IOErrorEvent;
    
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.ui.ContextMenu;
    import flash.ui.ContextMenuItem;
    import flash.Lib;
	using StringTools;

    class SVGViewer extends Sprite {
        
        public var xml(getXml, setXml) : XML ;
        public var svgRoot:SVGSVGNode;
        public var clipModeParam:String;
		public var layoutRootNode: SVGNode;
        // flag that indicates whether this viewer is in the middle of a 
        // suspendRedraw operation
        public var isSuspended:Bool ;

        var urlLoader:URLLoader;
        var context:ContextMenu;
        var objectWidth:Float;
        var objectHeight:Float;
		
        public function new() {
            
            isSuspended = false;
            customizeContextMenu();

            XML.ignoreProcessingInstructions = false;
            XML.ignoreComments = false;
            super();
			clipModeParam = "neither";
			objectHeight = Lib.current.stage.stageHeight;
			objectWidth = Lib.current.stage.stageWidth;
			
        }

        public function loadURL(url:String):Void {
            urlLoader = new URLLoader();
            urlLoader.load(new URLRequest(url));
            urlLoader.addEventListener(Event.COMPLETE, onComplete);
            urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
            //urlLoader.addEventListener(DynamicEvent.SECURITY_ERROR, onDynamic);
			trace(url);
        }
		
		public function setSize(w:Null<Float> = null, h:Null<Float> = null)
		{
			if (w != null)
				objectWidth = w;
			if (h != null)
				objectHeight = h;
			trace(objectWidth + ' x ' + objectHeight);
		}

        function onComplete(event:Event):Void {
            xml = new XML(urlLoader.data);
            urlLoader = null;
			/*graphics.beginFill(0x640000, 0.5);
			graphics.drawCircle(100, 100, 30);*/
        }

        function onIOError(event:IOErrorEvent):Void {
            trace("IOError: " + event.text);
            urlLoader = null;
        }

        function onDynamic(event:Dynamic):Void {
            trace("Dynamic: " + event.text);
            urlLoader = null;
        }

        public function getWidth():Float {
			//trace(objectWidth);
            return objectWidth;
        }

        public function getHeight():Float {
            return objectHeight;
        }
		
        public function getClipMode():String {
            return clipModeParam;
        }
		
        public function setXml(value:XML):XML {
            //trace(svgRoot +':' + value.toXMLString());
			if (svgRoot != null) {
                this.removeChild(svgRoot.topSprite);
            }
            svgRoot = new SVGSVGNode(null, value, null, this);
            //trace(this.name + ':' + svgRoot.topSprite.name + ' parent:' + parent.name);
            this.addChild(svgRoot.topSprite);
        	return value;
        }

        public function getXml():XML {
            return this.svgRoot.xml;
        }

        public function handleScript(script:String):Void {

        }

        public function addActionListener(eventType:String, target:EventDispatcher):Void {

        }

        public function removeActionListener(eventType:String, target:EventDispatcher):Void {

        } 
        
        public function customizeContextMenu():Void {
            context = new ContextMenu();
            
            // Compiler constants are used to specify the release name and
            // release number.  If the release name is not specified, none
            // will be shown.  If the release number is not specified, the
            // SVN revision of this source file is shown
            //var releaseName:String = ''+BUILD::releaseName;
            var releaseName:String = 'SVGViewer';
            if (releaseName.length!=0)
                releaseName += ' ';
            var releaseNumber:String = '0.1';
            //var releaseNumber:String = ''+BUILD::releaseNumber;
          //  if (releaseNumber.length == 0)
               // releaseNumber = "_S_Rev: 1183 _S_".replace(~/[^0-9]/g, "");
          /*  var itemAbout:ContextMenuItem = new ContextMenuItem("About SVG Web (" + releaseName + "r" + releaseNumber + ")");
            var itemBug:ContextMenuItem = new ContextMenuItem("Report Issue with SVG Web ");
            itemAbout.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, aboutSVGWeb);
            itemBug.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, reportBug);
            this.contextMenu = context;
            this.contextMenu.customItems.push(itemAbout);
            this.contextMenu.customItems.push(itemBug);

            function ():Void {
                navigateToURL(new URLRequest("http://code.google.com/p/svgweb/"));
            }
            function ():Void {
                navigateToURL(new URLRequest("http://code.google.com/p/svgweb/issues/list"));
            }*/
        }

        public function debug(debugMessage:String, ?info:PosInfos):Void {
			trace(debugMessage, info);
			//Out.dumpStack(Stack.callStack());
        }
        
        public function error(message:String):Void {
            this.debug(message);
        }
        
        /** Functions for profiling. */
        public function start(subject:String, ?subjectStarted:String = null):Void {
        }
        
        /** Functions for profiling. */
        public function end(subject:String, ?subjectStarted:String = null):Void {
        }
        
        /** Functions for profiling. */
        public function increment(subject:String, amount:Int):Void {
        }

    }
